local gamelib = require "club.threecard.gamelib"
local slotlib = require "club.threecard.slotlib"
local components = slotlib.require "components"

local ExecuteSystem = gamelib.entitas.ExecuteSystem
local class = gamelib.entitas.class
local Time = CS.UnityEngine.Time

local slot_system = class("slot_system", ExecuteSystem)

function slot_system:ctor(context, ingame)
    self.context = context;
    self.ingame = ingame;
    self.should_skip = false;
    slot_system.super.ctor(self, context);
end

function slot_system:wait(time)
    while time > 0 do
        if self.should_skip then
            return;
        end

        time = time - Time.deltaTime;
        gamelib.coroutine.step();
    end
end

function slot_system:step()
    gamelib.coroutine.step();
end

function slot_system:skip()
    self.should_skip = true;
end

function slot_system:cleanup()
    self.should_skip = false;
end

function slot_system:_execute()
    if self._collector.entities:size() > 0 then
        local interrupt_component = nil;
        self._collector.entities:foreach(function(entity)
            if entity:has(components.interrupt_component) then
                interrupt_component = entity:get(components.interrupt_component)
            end
        end)

        if interrupt_component then
            self:_finish();
        else
            slot_system.super._execute(self);
        end
    else
        self:_finish();
    end
end


return slot_system